﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Cngot.Extensions.Trace.Http
{
    public class HttpTraceOption
    {
        public bool Enable { get; set; } = true;
        public bool RequestBody { get; set; } = true;
        public bool ResponseBody { get; set; } = true;
        public int MaxRequestBodySize { get; set; } = 4096;
        public int MaxResponseBodySize { get; set; } = 4096;
        /// <summary>
        /// 排除URL地址
        /// </summary>
        public Func<HttpRequest, bool> ExcludeUrlDelegate { get; set; } = (request) =>
        {
            string path = request.Path;

            return path.StartsWith("/health") || path.StartsWith("/swagger");
        };

        public Func<HttpRequest, IDictionary<string, string>> RequestHeaderDelegate { get; set; } = (request) =>
        {
            return request.Headers.Where(w =>
                w.Key.ToLower().StartsWith("x-") ||
                w.Key.ToLower() == "user-agent" ||
                w.Key.ToLower() == "referer" ||
                w.Key.ToLower() == "authorization" ||
                w.Key.ToLower() == "traceparent"
            ).ToDictionary(w => w.Key, w => w.Value.FirstOrDefault());
        };
    }
}
